#include "CSetTextureCommand.h"
#include "CUserInterface.h"
#include "CLevelEditor.h"
#include "ISceneManager.h"
#include "ISceneNode.h"
#include "CSceneObject.h"
#include "IVideoDriver.h"

extern CGlobals* g;
extern CUserInterface* ui;
extern CLevelEditor* editor;

//used only for skybox object
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
CSetTextureCommand::CSetTextureCommand (int id, stringc prop, stringc file)
{
	m_id = id;
	
	CSceneObject * obj = editor->getSceneObjectFromID(m_id);
		
	GX_ASSERT(obj);
	
	m_prop		= 	prop;
	m_oldTexture = obj->m_UserData->getAttributeAsString(prop.c_str());	
	m_newTexture = file;
	
	setDescription(L"change texture");
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void CSetTextureCommand::redo ()
{
	CSceneObject * obj = editor->getSceneObjectFromID(m_id);
		
	GX_ASSERT(obj);

	obj->m_UserData->setAttribute(m_prop.c_str() , m_newTexture.c_str());
	
	createNode();
	
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void CSetTextureCommand::undo ()
{
	CSceneObject * obj = editor->getSceneObjectFromID(m_id);
		
	GX_ASSERT(obj);

	obj->m_UserData->setAttribute(m_prop.c_str() , m_oldTexture.c_str());

}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void CSetTextureCommand::createNode()
{
	CSceneObject * obj = editor->getSceneObjectFromID(m_id);
		
	GX_ASSERT(obj);

	stringc test = "TextureFile";
	stringc prop = "";

	bool allTextures = true;

	for (s32 i = 0 ; i < 6 ; i ++ )
	{
		prop = test;
		prop += i;

		if (obj->m_UserData->findAttribute(prop.c_str()) < 0 || obj->m_UserData->getAttributeAsString(prop.c_str()) == stringc(""))
		{
			allTextures = false;
			break;
		}
	}
	
	if (allTextures)
	{
		ISceneNode* parent = obj->m_SceneNode->getParent();

		if (!parent)
		{	
			parent = g->sceneManager->getRootSceneNode();
		}		
		
		IAttributes* attr = obj->m_UserData;

		ITexture* texture = g->driver->getTexture(attr->getAttributeAsString("TextureFile0")); 
		ITexture* texture1 = g->driver->getTexture(attr->getAttributeAsString("TextureFile1")); 
		ITexture* texture2 = g->driver->getTexture(attr->getAttributeAsString("TextureFile2")); 
		ITexture* texture3 = g->driver->getTexture(attr->getAttributeAsString("TextureFile3")); 
		ITexture* texture4 = g->driver->getTexture(attr->getAttributeAsString("TextureFile4")); 
		ITexture* texture5 = g->driver->getTexture(attr->getAttributeAsString("TextureFile5"));

		if (texture && texture1 && texture2 && texture3 && texture4 && texture5)
		{	
			editor->setSelectedNode(NULL);

			obj->m_SceneNode->remove();	
			obj->m_SceneNode = g->sceneManager->addSkyBoxSceneNode(texture,texture1,texture2,texture3,texture4,texture5);
			obj->m_SceneNode->setID(m_id);
			obj->m_SceneNode->grab();
			editor->setSelectedNode(obj->m_SceneNode);
		}
		else
		{
			DEBUG_OUT("Texture could not be loaded !!!");
		}
	}
}